from subprocess import Popen, PIPE

from pod.macros.html.util import tag
from pod.std import *

class GraphViz(Macro):
    name = "graphviz"
    doctype = "html"

    def __init__(self, env, scope, format="png", output=None):
        super(GraphViz, self).__init__()
        self.env = env
        self.format = format
        self.output = output

    def __call__(self, content):
        p = Popen(["dot", "-T%s" % self.format, "-o%s" % self.output], stdout=PIPE, stdin=PIPE)
        p.communicate(input=Macro.flatten(content))[0]
        return tag("img", src=self.output, endTag=False)
